﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.DAL;
using MINERP.MODEL.Statistics;
using MINERP.BLL.Statistics;
using MINERP.COM;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;

namespace MINERP.Statistics
{
    public partial class FrmShopBlance : Form
    {


        FinanceBLL _bll = new FinanceBLL();
        public static ShopEntity _shopEntity = null;
        public static FrmFinance frmFinance = null;
        public FrmShopBlance()
        {
            InitializeComponent();
        }


        private void FrmShopBlance_Load(object sender, EventArgs e)
        {
            LinkTechs();
            switch (_shopEntity.CheckOutType)
            {
                case 0:
                    cmbCheckOutType.Text = "预付款";
                    break;
                case 1:
                    cmbCheckOutType.Text = "信用额度";
                    txtCreditLimit.Text = _shopEntity.CreditLimit.ToString();
                    break;
                case 2:
                    cmbCheckOutType.Text = "现结";
                    break;
                case 3:
                    cmbCheckOutType.Text = "结账日期";
                    txtGraceDay.Text = _shopEntity.GraceDay.ToString();
                    txtOverDay.Text = _shopEntity.OverDay.ToString();
                    break;
                case 4:
                    cmbCheckOutType.Text = "结账日期与信用额度";
                    txtCreditLimit.Text = _shopEntity.CreditLimit.ToString();
                    txtGraceDay.Text = _shopEntity.GraceDay.ToString();
                    txtOverDay.Text = _shopEntity.OverDay.ToString();
                    break;
                case 5:
                    cmbCheckOutType.Text = "先结";
                    break;
                case 6:
                    cmbCheckOutType.Text = "代收";
                    txtGraceDay.Text = _shopEntity.GraceDay.ToString();
                    txtOverDay.Text = _shopEntity.OverDay.ToString();
                    break;
                default:
                    break;
            }
            LinkText(_shopEntity);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtCreditLimit.Focus();
                return;
            }
            if (CheckData())
            {
                if (_shopEntity != null)
                {
                    using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                    {
                        Shop shop = db.Shop.FirstOrDefault(sh => sh.ShopId == _shopEntity.ShopId);
                        //if (shop.CheckOutType == (int)CheckOutType.PrePay || shop.CheckOutType == (int)CheckOutType.CheckOutNow || IsAllBillCheckOut(shop,db))
                        //{
                        //if (shop.CheckOutType == (int)CheckOutType.PrePay || shop.CheckOutType == (int)CheckOutType.CheckOutNow )
                        //{
                            shop.CreditLimit = txtCreditLimit.Text.Trim() == string.Empty ? 0 : Math.Round(Convert.ToDecimal(txtCreditLimit.Text), 2);
                            //shop.OrderBalance = Math.Round((decimal)shop.Balance, 2);
                            shop.OverDay = txtOverDay.Text.Trim() == string.Empty ? 0 : Convert.ToInt32(txtOverDay.Text);
                            shop.GraceDay = txtGraceDay.Text.Trim() == string.Empty ? 0 : Convert.ToInt32(txtGraceDay.Text);
                            shop.AlarmDays = txtAlarmDays.Text.Trim() == string.Empty ? 0 : Convert.ToInt32(txtAlarmDays.Text);
                            shop.AlarmLimit = txtAlarmLimit.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtAlarmLimit.Text);
                            tb_Tech tech = _bll.GetTech(cmbAccountControlTech.Text);
                            if (tech != null)
                            {
                                shop.AccountControlTechId = tech.TechId;
                            }
                            else
                            {
                                shop.AccountControlTechId = 0;
                            }
                            switch (cmbCheckOutType.Text)
                            {
                                case "预付款":
                                    shop.CheckOutType = (int)CheckOutType.PrePay;
                                    break;
                                case "信用额度":
                                    shop.CheckOutType = (int)CheckOutType.CreditLimit;
                                    //shop.OrderBalance = shop.CreditLimit;
                                    break;
                                case "现结":
                                    shop.CheckOutType = (int)CheckOutType.CheckOutNow;
                                    //shop.OrderBalance = 0;
                                    break;
                                case "结账日期":
                                    shop.CheckOutType = (int)CheckOutType.DateTimeCheckOut;
                                    //shop.OrderBalance = 0;
                                    break;
                                case "结账日期与信用额度":
                                    shop.CheckOutType = (int)CheckOutType.CreditLimitAndDateTime;
                                    //shop.OrderBalance = shop.CreditLimit;
                                    break;
                                case "先结":
                                    shop.CheckOutType = (int)CheckOutType.FirstCheckOut;
                                    //shop.OrderBalance = 0;
                                    break;
                                case "代收":
                                    shop.CheckOutType = (int)CheckOutType.DelegateCheckOut;
                                    //shop.OrderBalance = 0;
                                    break;
                                default:
                                    break;

                            }
                            db.SaveChanges();

                            //refresh                    
                            //shop = _bll.GetShopObj(_shopEntity.ShopId);
                            LinkText(shop);

                            StringBuilder sb = new StringBuilder();
                            sb.Append(DateTime.Now.ToString());
                            sb.Append(" 操作员:");
                            sb.Append(FrmMain.loginName);
                            sb.Append(" 给商户:");
                            sb.Append(shop.shopName);
                            sb.Append(" 更改了结算类型,");
                            sb.Append(" 结算类型:");
                            sb.Append(cmbCheckOutType.Text);
                            sb.Append(" 信用额：");
                            sb.Append(txtCreditLimit.Text);
                            sb.Append(" 元");
                            sb.Append(" 结算日期:");
                            sb.Append(txtOverDay.Text);
                            sb.Append(" 号");
                            sb.Append(" 宽限天数:");
                            sb.Append(txtGraceDay.Text);
                            sb.Append(" 天");
                            BlanceOprationRecord.WriteLog(sb);
                            frmFinance.LinkDataGrid();

                            PrepareUpdate(false);

                            MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();

                        //}
                        //else
                        //{
                        //    MessageBox.Show("请先冲抵完该商户未结算的账单再更改结算类型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        //}
                    }

                }
                else
                {
                    MessageBox.Show("请选择要设置的商户！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }




        }

        private bool IsAllBillCheckOut(MINERP.MODEL.FoundationModel.Shop_Info shopInfo)
        {
            bool flag = false;
            if (_bll.GetBillList(shopInfo).Count == 0)
            {
                flag = true;
            }
            return flag;
        }
        private bool IsAllBillCheckOut(MINERP.MODEL.FoundationModel.Shop_Info shopInfo, newSampleYPDBEntities db)
        {
            bool flag = false;
            if (_bll.GetBillList(shopInfo, db).Count == 0)
            {
                flag = true;
            }
            return flag;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            //txtGraceDay.Text = string.Empty;
            //txtOverDay.Text = string.Empty;
            //txtCreditLimit.Text = string.Empty;
            //cmbCheckOutType.Text = string.Empty;
            txtCreditLimit.Enabled = false;
            txtOverDay.Enabled = false;
            txtGraceDay.Enabled = false;
            cmbCheckOutType.Enabled = false;
            btnUpdate.Text = "设置";
            btnUpdate.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性

        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;

            if (cmbCheckOutType.Text == string.Empty)
            {
                cmbCheckOutType.Focus();
                MessageBox.Show("结算类型不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbCheckOutType.Text.Contains("信用额") && txtCreditLimit.Text == string.Empty)
            {
                txtCreditLimit.Focus();
                MessageBox.Show("信用额不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if ((cmbCheckOutType.Text.Contains("结账日期") && txtOverDay.Text == string.Empty) || (cmbCheckOutType.Text.Contains("代收") && txtOverDay.Text == string.Empty))
            {
                txtOverDay.Focus();
                MessageBox.Show("结账日期不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if ((cmbCheckOutType.Text.Contains("结账日期") && txtOverDay.Text == string.Empty) || (cmbCheckOutType.Text.Contains("代收") && txtGraceDay.Text == string.Empty))
            {
                txtGraceDay.Focus();
                MessageBox.Show("宽限日期不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbCheckOutType.Text.Contains("结账日期"))
            {
                if ((Convert.ToInt32(txtOverDay.Text) + Convert.ToInt32(txtGraceDay.Text)) > 28)
                {
                    txtOverDay.Focus();
                    MessageBox.Show("结账日期与宽限日期纸和不能大于28天", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return flag;
        }

        /// <summary>
        /// 添加前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtCreditLimit.Enabled = bl;
            cmbCheckOutType.Enabled = bl;

            txtCreditLimit.Text = string.Empty;
            cmbCheckOutType.Text = string.Empty;


            if (bl)
            {
                btnUpdate.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置

        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtCreditLimit.Enabled = bl;
            txtOverDay.Enabled = bl;
            txtGraceDay.Enabled = bl;
            cmbCheckOutType.Enabled = bl;
            if (bl)
            {
                btnUpdate.Text = "保存";
            }
            else
            {
                btnUpdate.Text = "设置";
            }
        }

        private void txtCreditLimit_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                Convert.ToInt32(txtCreditLimit.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtCreditLimit.Text = string.Empty;
            }
        }

        private void cmbCheckOutType_Properties_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            txtGraceDay.Text = string.Empty;
            txtOverDay.Text = string.Empty;
            txtCreditLimit.Text = string.Empty;
            if (cmbCheckOutType.Text.Contains("结账日期") || cmbCheckOutType.Text.Contains("代收"))
            {
                panelDateTime.Visible = true;
            }
            else
            {
                panelDateTime.Visible = false;
            }
            if (cmbCheckOutType.Text.Contains("信用额"))
            {
                panelControlCreditLimit.Visible = true;
            }
            else
            {
                panelControlCreditLimit.Visible = false;
            }
        }

        private void cmbCheckOutType_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cmbCheckOutType.Text.Contains("结账日期") || cmbCheckOutType.Text.Contains("代收"))
            {
                panelDateTime.Visible = true;
            }
            else
            {
                panelDateTime.Visible = false;
            }
            if (cmbCheckOutType.Text.Contains("信用额"))
            {
                panelControlCreditLimit.Visible = true;
            }
            else
            {
                panelControlCreditLimit.Visible = false;
            }

        }

        private void LinkTechs()
        {
            cmbAccountControlTech.Properties.Items.Clear();
            foreach (var item in _bll.GetTechList())
            {
                cmbAccountControlTech.Properties.Items.Add(item.TechName);
            }
        }
        private void LinkText(Shop shop)
        {
            txtBlanceShow.Text = (shop.Balance == null ? 0 : Math.Round((decimal)shop.Balance, 2)).ToString();
            txtKeeper.Text = shop.shopKeeper;
            txtShopName.Text = shop.shopName;
            txtAlarmDays.Text = shop.AlarmDays.ToString();
            txtAlarmLimit.Text = shop.AlarmLimit.ToString();

            tb_Tech tech = _bll.GetTech(shop.AccountControlTechId);
            if (tech != null)
            {
                cmbAccountControlTech.Text = tech.TechName;
            }
            else
            {
                cmbAccountControlTech.Text = string.Empty;
            }
        }

        private void LinkText(ShopEntity shopEntity)
        {
            txtBlanceShow.Text = (shopEntity.Balance == null ? 0 : Math.Round((decimal)shopEntity.Balance, 2)).ToString();
            txtKeeper.Text = shopEntity.shopKeeper;
            txtShopName.Text = shopEntity.shopName;
            txtAlarmDays.Text = shopEntity.AlarmDays.ToString();
            txtAlarmLimit.Text = shopEntity.AlarmLimit.ToString();

            tb_Tech tech = _bll.GetTech(shopEntity.AccountControlTechId);
            if (tech != null)
            {
                cmbAccountControlTech.Text = tech.TechName;
            }
            else
            {
                cmbAccountControlTech.Text = string.Empty;
            }
        }
        /// <summary>
        /// 结算类型是否改变
        /// </summary>
        /// <param name="shop"></param>
        /// <returns></returns>
        private bool IsCheckOutTypeChanged(Shop shop)
        {
            bool flag = true;
            int checkOutType = 0;
            switch (cmbCheckOutType.Text)
            {
                case "预付款":
                    checkOutType = (int)CheckOutType.PrePay;
                    break;
                case "信用额度":
                    checkOutType = (int)CheckOutType.CreditLimit;
                    break;
                case "现结":
                    checkOutType = (int)CheckOutType.CheckOutNow;
                    break;
                case "结账日期":
                    checkOutType = (int)CheckOutType.DateTimeCheckOut;
                    break;
                case "结账日期与信用额度":
                    checkOutType = (int)CheckOutType.CreditLimitAndDateTime;
                    break;
                case "先结":
                    checkOutType = (int)CheckOutType.FirstCheckOut;
                    break;
                case "代收":
                    checkOutType = (int)CheckOutType.DelegateCheckOut;
                    break;
                default:
                    break;
            }
            if (shop.CheckOutType == checkOutType)
            {
                flag = false;
            }
            return flag;
        }
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
